package com.huafeng.controller;

import com.huafeng.annotations.Log;
import com.huafeng.commonutil.CheckUtils;
import com.huafeng.model.base.Result;
import com.huafeng.model.entity.SysPost;
import com.huafeng.model.enums.OperationTypeEnum;
import com.huafeng.model.enums.ResultCodeEnum;
import com.huafeng.model.exception.BusinessException;
import com.huafeng.model.vo.SysPostQueryVo;
import com.huafeng.service.SysPostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 花风
 * @date 2023/8/19 11:18
 * @Description SysPostController
 */
@Api(tags = "岗位管理")
@RestController
@RequestMapping("admin/system/sysPost")
public class SysPostController {
    @Resource
    private SysPostService sysPostService;

    @Log(model = "岗位管理",operationType = OperationTypeEnum.GET)
    @ApiOperation("分页条件查询岗位信息")
    @GetMapping("page")
    public Result querySysPostByPage(SysPostQueryVo sysPostQueryVo){
        return sysPostService.querySysPostByPage(sysPostQueryVo);
    }

    @Log(model = "岗位管理",operationType = OperationTypeEnum.GET)
    @ApiOperation("查看岗位详情信息")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id){
        Assert.isTrue(id != null && id > 0,"id必须不为空且大于0");
        SysPost sysPost = sysPostService.query().eq("id", id).one();
        if (sysPost == null){
            throw new BusinessException(ResultCodeEnum.DATA_ERROR);
        }
        return Result.ok(sysPost);
    }

    @Log(model = "岗位管理",operationType = OperationTypeEnum.DELETE)
    @ApiOperation("删除岗位")
    @DeleteMapping("remove/{id}")
    public Result removeById(@PathVariable Long id){
        Assert.isTrue(id != null && id > 0,"id必须不为空且大于0");
        boolean removeById = sysPostService.removeById(id);
        return CheckUtils.check(removeById);
    }

    @Log(model = "岗位管理",operationType = OperationTypeEnum.INSERT)
    @ApiOperation("新增岗位")
    @PostMapping("save")
    public Result save(@RequestBody SysPost sysPost){
        boolean save = sysPostService.save(sysPost);
        return CheckUtils.check(save);
    }

    @Log(model = "岗位管理",operationType = OperationTypeEnum.DELETE)
    @ApiOperation("批量删除岗位")
    @DeleteMapping("batchRemove")
    public Result removeBatch(@RequestBody List<Long> ids){
        Assert.isTrue(!CollectionUtils.isEmpty(ids),"id列表中必须有值");
        boolean removeByIds = sysPostService.removeByIds(ids);
        return CheckUtils.check(removeByIds);
    }

    @Log(model = "岗位管理",operationType = OperationTypeEnum.STATUS)
    @ApiOperation("更新状态")
    @GetMapping("updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id,@PathVariable Integer status){
        Assert.isTrue(id != null && id > 0,"id必须不为空且大于0");
        Assert.isTrue(status != null && status >= 0,"status不合法");

        return sysPostService.updateStatus(id,status);
    }
}
